package com.bjsxt.userCenter.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bjsxt.userCenter.common.controller.BaseController;
import com.bjsxt.userCenter.common.util.ConstantFinalUtil;
import com.bjsxt.userCenter.common.util.EncryUtil;
import com.bjsxt.userCenter.user.service.pojo.AAdmins;
import com.bjsxt.userCenter.user.service.pojo.AdminEnum;
import com.bjsxt.userCenter.user.service.service.IUserService;
/**
 * 未登陆的页面
 * @author BlackMA
 *
 */
@Controller
public class NoLoginController extends BaseController
{
	@Resource
	private IUserService userService;
	
	/**
	 * 登陆页
	 * @param request
	 * @return
	 */
	@RequestMapping("/login")
	public String login(HttpServletRequest request)
	{
		/*获取returnUrl*/
		String returnUrl = request.getParameter("returnUrl");
		request.setAttribute("returnUrl", returnUrl);
		return "/login";
	}
	
	/**
	 * 登陆提交
	 * @param request
	 * @param email
	 * @param password
	 * @return
	 */
	@RequestMapping("/loginSubmit")
	public String loginSubmit(HttpServletRequest request,String email,String password)
	{
		HttpSession session = request.getSession();
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.put("email", email);
		/*先按照email进行查询，具体确定邮箱不正确还是密码不正确，将正确的邮箱放到页面上，更友好*/
		AAdmins admin = this.userService.findOneAAdmins(condMap);
		if(admin != null)
		{
			/*邮箱正确,验证密码*/
			if(EncryUtil.checkPassword(password, admin.getPassword()))
			{
				/*密码正确,验证启用禁用*/
				if(AdminEnum.checkEnable(admin.getStatus()))
				{
					/*登陆成功,跳转到登陆后的页面*/
					ConstantFinalUtil.LOGGER.info("登陆成功");
					session.setAttribute("eamil", email);
					admin.setLastLoginTime(new Date());
					this.userService.updateOneAAdmins(admin);
					session.setAttribute("lastLoginTime", new Date());
					return "redirect:/back/admins/main.htm";
				}else
				{
					/*用户被禁用,请联系管理员*/
					this.info = ConstantFinalUtil.codeInfoJSON.getString("4");
				}
			}else
			{
				/*密码不正确*/
				this.info = ConstantFinalUtil.codeInfoJSON.getString("5");
			}
			request.setAttribute("email", email);
		}else
		{
			/*邮箱不正确*/
			this.info = ConstantFinalUtil.codeInfoJSON.getString("6");
		}
		request.setAttribute("info", info);
		return "login";
	}
}
